package com.chkj.fbook.common.utils;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @TODO: 生成UUID工具类
 * @author: shh_he
 */
public class IDUtils {
	private static Map<String, Object> map = Collections.synchronizedMap(new HashMap<String, Object>());
	private static String NUMBER = "number";

	public static synchronized String genID() {
		if (map.get(NUMBER) == null) {
			map.put(NUMBER, new Integer(0));
		}
		String id = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		int number = ((Integer) map.get(NUMBER)).intValue();
		String no = "" + number;
		if (number == 9999) {
			number = 0;
		} else {
			number++;
		}
		map.put(NUMBER, new Integer(number));
		no = no.trim();
		int s = 4 - no.length();
		for (int i = 0; i < s; i++) {
			no = "0" + no;
		}
		id += no;
		return id;
	}

	public static String genNo(long value, int m) {
		Map<String, String> map = new LinkedHashMap<String, String>();
		for (int i = 1; i <= 36; i++) {
			if (i <= 9) {
				map.put(i + "", i + "");
			} else if (i == 10) {
				map.put(i + "", "0");
			} else {
				map.put(i + "", (char) (i + 55) + "");
			}
		}
		String no = "";
		while (value >= m) {
			no = map.get((value % m) + "") + no;
			value = value / m;
		}
		no = map.get(value + "") + no;
		return no;
	}

	public static synchronized String genUUID() {
		String key = UUID.randomUUID().toString();
		key = key.replace("-", "");
		return key.toUpperCase();
	}

	public static void main(String[] args) {
		System.out.println(genUUID());
	}

}
